#include <QLabel>
#include <QHBoxLayout>
#include <QDebug>
#include <QtGui>
#include <QSplitter>
#include <QTextBrowser>
#include "widgetBase.h"
//#include "myButton.h"
#include <QDateTime>
#include <QMessageBox>
#include <cmath>
#include <QFontDatabase>
#include <math.h>
#include <QTimer>

#define PI 3.1415927

#define LABELWIDTH 100
#define LABELHIGHTH 30

extern void voice();

void setView(QComboBox* target)
{
    QListView* view = new QListView();
    view->verticalScrollBar()->setStyleSheet("QScrollBar{background:transparent; width: 30px;}"
                      "QScrollBar::handle{background-color:darkgray;min-height: 20px;}"
                      "QScrollBar::handle:pressed{background:gray;}"
                      "QScrollBar::sub-line{background:transparent;}"
                      "QScrollBar::add-line{background:transparent;}");
    QSize size = view->sizeHint();
    size.setHeight(30);
    view->setGridSize(size);

    target->setView(view);
}

widgetBasewave::widgetBasewave(QWidget *parent) :
    QWidget(parent)
{
    QFont ft1;
    ft1.setPixelSize(18);
    ft1.setBold(true);

    QFont ft;

    ft.setPixelSize(18);

    QLabel *labStyle_UA= new QLabel("UA:");
    labStyle_UA->setFixedSize(30,40);
    labStyle_UA->setFont(ft);

//    QListView* viewUA = new QListView();
//    QSize size = viewUA->sizeHint();
//    size.setHeight(30);
//    viewUA->setGridSize(size);

    cboVoltageUA = new QComboBox();
    cboVoltageUA->setFixedSize(100,40);
    cboVoltageUA->setFont(ft);
    setView(cboVoltageUA);

    QStringList styleLineVoltageUA = QStringList() <<" 100 "<<" 100/√3 ";
    cboVoltageUA->addItems(styleLineVoltageUA);
    cboVoltageUA->setCurrentIndex(0);

    QLabel *labLineVoltageUA_dw = new QLabel("V");
    labLineVoltageUA_dw->setFixedSize(30,40);
    labLineVoltageUA_dw->setFont(ft);

    lineUAn = new QLineEdit("0");
    lineUAn->setFixedSize(100,40);
    lineUAn->setFont(ft);
    lineUAn->setAlignment(Qt::AlignCenter);
    lineUAn->installEventFilter(this);

    QLabel *lablineUA_dw = new QLabel("%");
    lablineUA_dw->setFixedSize(20,40);
    lablineUA_dw->setFont(ft);

    LineUA = new QLineEdit("0");
    LineUA->setFixedSize(100,40);
    LineUA->setFont(ft);
    LineUA->setAlignment(Qt::AlignCenter);
    LineUA->installEventFilter(this);

    labRangeUA_dw = new QLabel("°");
    labRangeUA_dw->setFixedSize(20,40);
    labRangeUA_dw->setFont(ft);

    QHBoxLayout *layoutVoltageA = new QHBoxLayout;
    layoutVoltageA->addWidget(labStyle_UA);
    layoutVoltageA->addWidget(cboVoltageUA);
    layoutVoltageA->addWidget(labLineVoltageUA_dw);
    layoutVoltageA->addWidget(lineUAn);
    layoutVoltageA->addWidget(lablineUA_dw);
    layoutVoltageA->addWidget(LineUA);
    layoutVoltageA->addWidget(labRangeUA_dw);

    QLabel *labStyle_IA= new QLabel("IA:");
    labStyle_IA->setFixedSize(50,40);
    labStyle_IA->setFont(ft);

    cboCurrentIA = new QComboBox;
    cboCurrentIA->addItem("5");
    cboCurrentIA->addItem("1");
    cboCurrentIA->setFixedSize(80,40);
    cboCurrentIA->setFont(ft);
    setView(cboCurrentIA);

    QLabel *labLineCurrentIA_dw = new QLabel("A");
    labLineCurrentIA_dw->setFixedSize(30,40);
    labLineCurrentIA_dw->setFont(ft);

    lineIAn = new QLineEdit("0");
    lineIAn->setFixedSize(100,40);
    lineIAn->setFont(ft);
    lineIAn->setAlignment(Qt::AlignCenter);
    lineIAn->installEventFilter(this);

    QLabel *lablineIA_dw = new QLabel("%");
    lablineIA_dw->setFixedSize(20,40);
    lablineIA_dw->setFont(ft);

    LineIA = new QLineEdit("0");
    LineIA->setFixedSize(100,40);
    LineIA->setFont(ft);
    LineIA->setAlignment(Qt::AlignCenter);
    LineIA->installEventFilter(this);

    labRangeIA_dw = new QLabel("°");
    labRangeIA_dw->setFixedSize(20,40);
    labRangeIA_dw->setFont(ft);

    QHBoxLayout *layoutCurrentA = new QHBoxLayout;
    layoutCurrentA->addWidget(labStyle_IA);
    layoutCurrentA->addWidget(cboCurrentIA);
    layoutCurrentA->addWidget(labLineCurrentIA_dw);
    layoutCurrentA->addWidget(lineIAn);
    layoutCurrentA->addWidget(lablineIA_dw);
    layoutCurrentA->addWidget(LineIA);
    layoutCurrentA->addWidget(labRangeIA_dw);

    //second line for UB and IB
    QLabel *labStyle_UB= new QLabel("UB:");
    labStyle_UB->setFixedSize(50,40);
    labStyle_UB->setFont(ft);

    cboVoltageUB = new QComboBox();
    cboVoltageUB->setFixedSize(100,40);
    cboVoltageUB->setFont(ft);
    setView(cboVoltageUB);

    //QStringList styleLineVoltageUB = QStringList() <<" 110/√3 ";
    QStringList styleLineVoltageUB = QStringList() <<" 100 "<<" 100/√3 ";
    cboVoltageUB->addItems(styleLineVoltageUB);
    cboVoltageUB->setCurrentIndex(0);

    QLabel *labLineVoltageUB_dw = new QLabel("V");
    labLineVoltageUB_dw->setFixedSize(30,40);
    labLineVoltageUB_dw->setFont(ft);

    lineUBn = new QLineEdit("0");
    lineUBn->setFixedSize(100,40);
    lineUBn->setFont(ft);
    lineUBn->setAlignment(Qt::AlignCenter);
    lineUBn->installEventFilter(this);

    QLabel *lablineUB_dw = new QLabel("%");
    lablineUB_dw->setFixedSize(20,40);
    lablineUB_dw->setFont(ft);

    LineUB = new QLineEdit("240");
    LineUB->setFixedSize(100,40);
    LineUB->setFont(ft);
    LineUB->setAlignment(Qt::AlignCenter);
    LineUB->installEventFilter(this);

    labRangeUB_dw = new QLabel("°");
    labRangeUB_dw->setFixedSize(20,40);
    labRangeUB_dw->setFont(ft);

    QHBoxLayout *layoutVoltageB = new QHBoxLayout;
    layoutVoltageB->addWidget(labStyle_UB);
    layoutVoltageB->addWidget(cboVoltageUB);
    layoutVoltageB->addWidget(labLineVoltageUB_dw);
    layoutVoltageB->addWidget(lineUBn);
    layoutVoltageB->addWidget(lablineUB_dw);
    layoutVoltageB->addWidget(LineUB);
    layoutVoltageB->addWidget(labRangeUB_dw);

    QLabel *labStyle_IB= new QLabel("IB:");
    labStyle_IB->setFixedSize(50,40);
    labStyle_IB->setFont(ft);

    cboCurrentIB = new QComboBox;
    cboCurrentIB->addItem("5");
    cboCurrentIB->addItem("1");
    cboCurrentIB->setFixedSize(80,40);
    cboCurrentIB->setFont(ft);
    setView(cboCurrentIB);

    QLabel *labLineCurrentIB_dw = new QLabel("A");
    labLineCurrentIB_dw->setFixedSize(30,40);
    labLineCurrentIB_dw->setFont(ft);

    lineIBn = new QLineEdit("0");
    lineIBn->setFixedSize(100,40);
    lineIBn->setFont(ft);
    lineIBn->setAlignment(Qt::AlignCenter);
    lineIBn->installEventFilter(this);

    QLabel *lablineIB_dw = new QLabel("%");
    lablineIB_dw->setFixedSize(20,40);
    lablineIB_dw->setFont(ft);

    LineIB = new QLineEdit("240");
    LineIB->setFixedSize(100,40);
    LineIB->setFont(ft);
    LineIB->setAlignment(Qt::AlignCenter);
    LineIB->installEventFilter(this);

    labRangeIB_dw = new QLabel("°");
    labRangeIB_dw->setFixedSize(20,40);
    labRangeIB_dw->setFont(ft);

    QHBoxLayout *layoutCurrentB = new QHBoxLayout;
    layoutCurrentB->addWidget(labStyle_IB);
    layoutCurrentB->addWidget(cboCurrentIB);
    layoutCurrentB->addWidget(labLineCurrentIB_dw);
    layoutCurrentB->addWidget(lineIBn);
    layoutCurrentB->addWidget(lablineIB_dw);
    layoutCurrentB->addWidget(LineIB);
    layoutCurrentB->addWidget(labRangeIB_dw);

    //thired line for UC and IC
    QLabel *labStyle_UC= new QLabel("UC:");
    labStyle_UC->setFixedSize(50,40);
    labStyle_UC->setFont(ft);

    cboVoltageUC = new QComboBox();
    cboVoltageUC->setFixedSize(100,40);
    cboVoltageUC->setFont(ft);
    setView(cboVoltageUC);

    QStringList styleLineVoltageUC = QStringList() <<" 100 " <<" 100/√3 ";
    cboVoltageUC->addItems(styleLineVoltageUC);
    cboVoltageUC->setCurrentIndex(0);

    QLabel *labLineVoltageUC_dw = new QLabel("V");
    labLineVoltageUC_dw->setFixedSize(30,40);
    labLineVoltageUC_dw->setFont(ft);

    lineUCn = new QLineEdit("0");
    lineUCn->setFixedSize(100,40);
    lineUCn->setFont(ft);
    lineUCn->setAlignment(Qt::AlignCenter);
    lineUCn->installEventFilter(this);

    QLabel *lablineUC_dw = new QLabel("%");
    lablineUC_dw->setFixedSize(20,40);
    lablineUC_dw->setFont(ft);

    LineUC = new QLineEdit("120");
    LineUC->setFixedSize(100,40);
    LineUC->setFont(ft);
    LineUC->setAlignment(Qt::AlignCenter);
    LineUC->installEventFilter(this);

    labRangeUC_dw = new QLabel("°");
    labRangeUC_dw->setFixedSize(20,40);
    labRangeUC_dw->setFont(ft);

    QHBoxLayout *layoutVoltageC = new QHBoxLayout;
    layoutVoltageC->addWidget(labStyle_UC);
    layoutVoltageC->addWidget(cboVoltageUC);
    layoutVoltageC->addWidget(labLineVoltageUC_dw);
    layoutVoltageC->addWidget(lineUCn);
    layoutVoltageC->addWidget(lablineUC_dw);
    layoutVoltageC->addWidget(LineUC);
    layoutVoltageC->addWidget(labRangeUC_dw);

    QLabel *labStyle_IC= new QLabel("IC:");
    labStyle_IC->setFixedSize(50,40);
    labStyle_IC->setFont(ft);

    cboCurrentIC = new QComboBox;
    cboCurrentIC->addItem("5");
    cboCurrentIC->addItem("1");
    cboCurrentIC->setFixedSize(80,40);
    cboCurrentIC->setFont(ft);
    setView(cboCurrentIC);

    QLabel *labLineCurrentIC_dw = new QLabel("A");
    labLineCurrentIC_dw->setFixedSize(30,40);
    labLineCurrentIC_dw->setFont(ft);

    lineICn = new QLineEdit("0");
    lineICn->setFixedSize(100,40);
    lineICn->setFont(ft);
    lineICn->setAlignment(Qt::AlignCenter);
    lineICn->installEventFilter(this);

    QLabel *lablineIC_dw = new QLabel("%");
    lablineIC_dw->setFixedSize(20,40);
    lablineIC_dw->setFont(ft);

    LineIC = new QLineEdit("120");
    LineIC->setFixedSize(100,40);
    LineIC->setFont(ft);
    LineIC->setAlignment(Qt::AlignCenter);
    LineIC->installEventFilter(this);

    labRangeIC_dw = new QLabel("°");
    labRangeIC_dw->setFixedSize(20,40);
    labRangeIC_dw->setFont(ft);

    QHBoxLayout *layoutCurrentC = new QHBoxLayout;
    layoutCurrentC->addWidget(labStyle_IC);
    layoutCurrentC->addWidget(cboCurrentIC);
    layoutCurrentC->addWidget(labLineCurrentIC_dw);
    layoutCurrentC->addWidget(lineICn);
    layoutCurrentC->addWidget(lablineIC_dw);
    layoutCurrentC->addWidget(LineIC);
    layoutCurrentC->addWidget(labRangeIC_dw);

    QVBoxLayout *layoutLeft = new QVBoxLayout;
    layoutLeft->addLayout(layoutVoltageA);
    layoutLeft->addSpacing(5);
    layoutLeft->addLayout(layoutVoltageB);
    layoutLeft->addSpacing(5);
    layoutLeft->addLayout(layoutVoltageC);

    QVBoxLayout *layoutRight = new QVBoxLayout;
    layoutRight->addLayout(layoutCurrentA);
    layoutRight->addSpacing(5);
    layoutRight->addLayout(layoutCurrentB);
    layoutRight->addSpacing(5);
    layoutRight->addLayout(layoutCurrentC);

    QHBoxLayout *layoutTestDown = new QHBoxLayout;
    layoutTestDown->addStretch(1);
    layoutTestDown->addLayout(layoutLeft);
    layoutTestDown->addSpacing(20);
    layoutTestDown->addLayout(layoutRight);
    layoutTestDown->addStretch(1);

    QLabel *labFrequency = new QLabel("频率：");

    Frequency = new QLineEdit("50");
    Frequency->setFixedSize(100,30);
    Frequency->setFont(ft);
    Frequency->setAlignment(Qt::AlignCenter);

    QLabel *labFrequency_dw = new QLabel("Hz");
    labFrequency_dw->setFixedSize(30,30);
    labFrequency_dw->setFont(ft);

    QHBoxLayout *layoutFrequency = new QHBoxLayout;
    layoutFrequency->addWidget(labFrequency);
    layoutFrequency->addWidget(Frequency);
    layoutFrequency->addWidget(labFrequency_dw);

    QLabel *labPQuick = new QLabel("功因数快速设置：");
    btnP1 = new QPushButton("0.5L");
    btnP2 = new QPushButton("0.8L");
    btnP3 = new QPushButton("1.0");
    btnP4 = new QPushButton("0.5C");
    btnP5 = new QPushButton("0.8C");

    QLabel *labUQuick = new QLabel("电压快速设置：");
    btnU1 = new QPushButton("0%");
    btnU2 = new QPushButton("20%");
    btnU3 = new QPushButton("50%");
    btnU4 = new QPushButton("80%");
    btnU5 = new QPushButton("100%");
    btnU6 = new QPushButton("120%");

    funcBtnUList<<btnU1<<btnU2<<btnU3<<btnU4<<btnU5<<btnU6;

    funcBtnPList<<btnP1<<btnP2<<btnP3<<btnP4<<btnP5;

    QHBoxLayout *layoutUQuick = new QHBoxLayout;
    layoutUQuick->addWidget(labUQuick);
    layoutUQuick->addWidget(btnU1);
    layoutUQuick->addWidget(btnU2);
    layoutUQuick->addWidget(btnU3);
    layoutUQuick->addWidget(btnU4);
    layoutUQuick->addWidget(btnU5);
    layoutUQuick->addWidget(btnU6);
    layoutUQuick->addStretch(1);
    layoutUQuick->addWidget(labPQuick);
    layoutUQuick->addWidget(btnP1);
    layoutUQuick->addWidget(btnP2);
    layoutUQuick->addWidget(btnP3);
    layoutUQuick->addWidget(btnP4);
    layoutUQuick->addWidget(btnP5);

    QLabel *labIQuick = new QLabel("电流快速设置：");
    btnI1 = new QPushButton("0%");
    btnI2 = new QPushButton("1%");
    btnI3 = new QPushButton("5%");
    btnI4 = new QPushButton("20%");
    btnI5 = new QPushButton("50%");
    btnI6 = new QPushButton("100%");
    btnI7 = new QPushButton("120%");

    funcBtnIList<<btnI1<<btnI2<<btnI3<<btnI4<<btnI5<<btnI6<<btnI7;

    QHBoxLayout *layoutIQuick = new QHBoxLayout;
    layoutIQuick->addWidget(labIQuick);
    layoutIQuick->addWidget(btnI1);
    layoutIQuick->addWidget(btnI2);
    layoutIQuick->addWidget(btnI3);
    layoutIQuick->addWidget(btnI4);
    layoutIQuick->addWidget(btnI5);
    layoutIQuick->addWidget(btnI6);
    layoutIQuick->addWidget(btnI7);
    layoutIQuick->addStretch(1);
    layoutIQuick->addLayout(layoutFrequency);

    ft.setPixelSize(18);
    QVBoxLayout *layoutTestCfg = new QVBoxLayout;
    layoutTestCfg->addLayout(layoutUQuick);
    layoutTestCfg->addLayout(layoutIQuick);
    layoutTestCfg->addSpacing(20);
    layoutTestCfg->addLayout(layoutTestDown);
    layoutTestCfg->addStretch(1);

    QWidget *boxTestCfg = new QWidget;
    boxTestCfg->setLayout(layoutTestCfg);
    boxTestCfg->setFixedWidth(1000);
    boxTestCfg->setStyleSheet(".QWidget{border-radius:5px;font-size:20px;}QGroupBox:title{color:darkblue;}");

    QLabel *labBlank = new QLabel();
    labBlank->setStyleSheet(".QLabel{background-color:darkgray;height:2px;}");
    labBlank->setFixedSize(1000,2);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(boxTestCfg);
    mainLayout->addStretch(1);
    mainLayout->addWidget(labBlank);
    mainLayout->addStretch(1);
    setLayout(mainLayout);

    //widget info
    widgetInfo = new QWidget(this);
    widgetInfo->setStyleSheet(".QWidget{background-color:qColor(250,250,0,150);border-radius:15px;}");
    labInfo = new QLabel("欢迎使用...");
    labInfo->setStyleSheet("color:yellow;font:22px;font-weight:bold;");
    labInfo->setFixedSize(280,50);
    labInfo->setAlignment(Qt::AlignCenter);

    QVBoxLayout *layoutInfo = new QVBoxLayout;
    layoutInfo->setAlignment(Qt::AlignCenter);
    layoutInfo->addWidget(labInfo);

    widgetInfo->setLayout(layoutInfo);
    widgetInfo->setFixedSize(450,60);
    widgetInfo->move(370,260);

    QTimer::singleShot(1000, this, SLOT(widgetInfoHide()));

    TestStyle = false;

    lineActive = NULL;

    init();
}

void widgetBasewave::Line_change(QString str)
{
    qDebug()<<"PAGEHUMITURE::Line_change"<<str;
    QLineEdit *current_lineEdit = (QLineEdit *)sender();
    float show_str = 0.0;

    show_str = current_lineEdit->text().toFloat();

    if(current_lineEdit == LineUA || current_lineEdit == LineUB || current_lineEdit == LineUC
            || current_lineEdit == LineIA || current_lineEdit == LineIB || current_lineEdit == LineIC)
    {
        if(show_str < 0.0 || show_str > 360.0)
        {
            QMessageBox::warning(this,tr("错误"),QString("数据输入错误，请输入0～360的数据！"),QMessageBox::tr("确定"));
            current_lineEdit->clear();
        }
    }
    else if(current_lineEdit == lineUAn || current_lineEdit == lineUBn || current_lineEdit == lineUCn)
    {
        if(show_str < 0.0 || show_str > 120.0)
        {
            QMessageBox::warning(this,tr("错误"),QString("数据输入错误，请输入0～120的数据！"),QMessageBox::tr("确定"));
            current_lineEdit->setText("");
        }
    }
    else if(current_lineEdit == lineIAn || current_lineEdit == lineIBn || current_lineEdit == lineICn )
    {
        if(show_str < 0.0 || show_str > 120.0)
        {
            QMessageBox::warning(this,tr("错误"),QString("数据输入错误，请输入0～120的数据！"),QMessageBox::tr("确定"));
            current_lineEdit->setText("");
        }
    }
}

QString widgetBasewave::Cos_Calculation(int Style, QString data_str)
{
    double value = 0.0;
    double tmpAngle = 1;
    double tmpArcCos = 0.0;
    QString return_str = "";
    QString tmp_str = "";

    if(Style == 0) //设置角度
    {
        value = data_str.toDouble();
        value = value * PI / 180;
        if(value < 0)
        {
            value = cos(value);
            return_str = QString::number(value,'f',2)+"C";
        }
        else
        {
            value = cos(value);
            return_str = QString::number(value,'f',2)+"L";
        }
        return return_str;
    }
    else if(Style == 1) //设置余弦值
    {
        tmp_str = data_str;

        tmp_str = tmp_str.mid(tmp_str.length() -1);

        if(tmp_str == "L" || tmp_str == "l")
        {
            data_str = data_str.mid(0,data_str.length()-1);

            qDebug()<<"Cos_Calculation(),data_str"<<data_str;
        }
        else if(tmp_str == "C" || tmp_str == "c")
        {
            data_str = data_str.mid(0,data_str.length()-1);

            tmpAngle = -1;

            qDebug()<<"Cos_Calculation(),data_str"<<data_str;
        }
        else
        {
            data_str = data_str;
        }

        tmpArcCos = data_str.toDouble();

        if(tmpArcCos <= 1
                && tmpArcCos >= -1)
        {
            tmpAngle *= acos(tmpArcCos);
            tmpAngle = tmpAngle * 180 /PI;
            return_str = QString::number(tmpAngle,'f',1);
        }
        else
        {
            return_str = "err";
        }
        return return_str;
    }
    return 0;
}

void widgetBasewave::setDataStyle(QLabel *lab)
{
    QFont ft;
    ft.setPixelSize(20);

    lab->setFixedSize(100,40);
    lab->setFont(ft);
    lab->setAlignment(Qt::AlignCenter);

    lab->setLineWidth(1);
    lab->setFrameStyle(QFrame::Box|QFrame::Raised);
//    lab->setStyleSheet("background-color:#ddd;color:darkgreen;");
    lab->setStyleSheet("background-color:#ddd;color:darkgreen;border: 1px solid #636363;border-radius: 5px;padding: 2px;border-color:#ddd;");
}

void widgetBasewave::boxChangeTo(int index)
{
    Q_UNUSED(index)

    QComboBox *current_combox = (QComboBox *)sender();

    QString tem = current_combox->currentText();
    float temp = 0;
    float tema = 0;
    float temb = 0;
    float temc = 0;

    if(current_combox == boxVoltage)
    {
        /*if(lineActive == lineUAn)*/ lineUAn->setText(tem);
        /*else if(lineActive == lineUBn) */lineUBn->setText(tem);
        /*else if(lineActive == lineUCn) */lineUCn->setText(tem);
//        boxVoltage->setCurrentIndex(0);
    }
    else if(current_combox == boxCurrent)
    {
       /* if(lineActive == lineIAn) */lineIAn->setText(tem);
        /*else if(lineActive == lineIBn) */lineIBn->setText(tem);
        /*else if(lineActive == lineICn) */lineICn->setText(tem);
//        boxCurrent->setCurrentIndex(0);

    }
    else if(current_combox == boxPhase)
    {
        if(current_combox->currentIndex() == 0) temp = 60.00;
        else if(current_combox->currentIndex() == 1) temp = 36.87;
        else if(current_combox->currentIndex() == 2) temp = 0;
        else if(current_combox->currentIndex() == 3) temp = -60.00;
        else if(current_combox->currentIndex() == 4) temp = -36.87;

        tema = LineUA->text().toFloat() - temp;
        temb = LineUB->text().toFloat() - temp;
        temc = LineUC->text().toFloat() - temp;

        if(tema < 0) tema += 360;
        if(temb < 0) temb += 360;
        if(temc < 0) temc += 360;

        LineIA->setText(QString::number(tema,'f',2));
        LineIB->setText(QString::number(temb,'f',2));
        LineIC->setText(QString::number(temc,'f',2));
    }
}

void widgetBasewave::init()
{
    connect(lineUAn,SIGNAL(textEdited(QString)),this,SLOT(Line_change(QString)));
    connect(lineUBn,SIGNAL(textEdited(QString)),this,SLOT(Line_change(QString)));
    connect(lineUCn,SIGNAL(textEdited(QString)),this,SLOT(Line_change(QString)));
    connect(lineIAn,SIGNAL(textEdited(QString)),this,SLOT(Line_change(QString)));
    connect(lineIBn,SIGNAL(textEdited(QString)),this,SLOT(Line_change(QString)));
    connect(lineICn,SIGNAL(textEdited(QString)),this,SLOT(Line_change(QString)));

    connect(LineUA,SIGNAL(textEdited(QString)),this,SLOT(Line_change(QString)));
    connect(LineUB,SIGNAL(textEdited(QString)),this,SLOT(Line_change(QString)));
    connect(LineUC,SIGNAL(textEdited(QString)),this,SLOT(Line_change(QString)));
    connect(LineIA,SIGNAL(textEdited(QString)),this,SLOT(Line_change(QString)));
    connect(LineIB,SIGNAL(textEdited(QString)),this,SLOT(Line_change(QString)));
    connect(LineIC,SIGNAL(textEdited(QString)),this,SLOT(Line_change(QString)));

    QSignalMapper* UMapper = new QSignalMapper(this);
    connect(UMapper, SIGNAL(mapped(const QString&)), this, SLOT(UQuickSet(const QString&)));

    UMapper->setMapping( btnU1, "0");
    connect(btnU1, SIGNAL(clicked(bool)), UMapper, SLOT(map()));
    UMapper->setMapping( btnU2, "20");
    connect(btnU2, SIGNAL(clicked(bool)), UMapper, SLOT(map()));
    UMapper->setMapping( btnU3, "50");
    connect(btnU3, SIGNAL(clicked(bool)), UMapper, SLOT(map()));
    UMapper->setMapping( btnU4, "80");
    connect(btnU4, SIGNAL(clicked(bool)), UMapper, SLOT(map()));
    UMapper->setMapping( btnU5, "100");
    connect(btnU5, SIGNAL(clicked(bool)), UMapper, SLOT(map()));
    UMapper->setMapping( btnU6, "120");
    connect(btnU6, SIGNAL(clicked(bool)), UMapper, SLOT(map()));

    QSignalMapper* IMapper = new QSignalMapper(this);
    connect(IMapper, SIGNAL(mapped(const QString&)), this, SLOT(IQuickSet(const QString&)));

    IMapper->setMapping( btnI1, "0");
    connect(btnI1, SIGNAL(clicked(bool)), IMapper, SLOT(map()));
    IMapper->setMapping( btnI2, "1");
    connect(btnI2, SIGNAL(clicked(bool)), IMapper, SLOT(map()));
    IMapper->setMapping( btnI3, "5");
    connect(btnI3, SIGNAL(clicked(bool)), IMapper, SLOT(map()));
    IMapper->setMapping( btnI4, "20");
    connect(btnI4, SIGNAL(clicked(bool)), IMapper, SLOT(map()));
    IMapper->setMapping( btnI5, "50");
    connect(btnI5, SIGNAL(clicked(bool)), IMapper, SLOT(map()));
    IMapper->setMapping( btnI6, "100");
    connect(btnI6, SIGNAL(clicked(bool)), IMapper, SLOT(map()));
    IMapper->setMapping( btnI7, "120");
    connect(btnI7, SIGNAL(clicked(bool)), IMapper, SLOT(map()));

    QSignalMapper* PMapper = new QSignalMapper(this);
    connect(PMapper, SIGNAL(mapped(const QString&)), this, SLOT(PQuickSet(const QString&)));

    PMapper->setMapping( btnP1, "60.00");
    connect(btnP1, SIGNAL(clicked(bool)), PMapper, SLOT(map()));
    PMapper->setMapping( btnP2, "36.87");
    connect(btnP2, SIGNAL(clicked(bool)), PMapper, SLOT(map()));
    PMapper->setMapping( btnP3, "0");
    connect(btnP3, SIGNAL(clicked(bool)), PMapper, SLOT(map()));
    PMapper->setMapping( btnP4, "-60.00");
    connect(btnP4, SIGNAL(clicked(bool)), PMapper, SLOT(map()));
    PMapper->setMapping( btnP5, "-36.87");
    connect(btnP5, SIGNAL(clicked(bool)), PMapper, SLOT(map()));
}

void widgetBasewave::recvResult(QByteArray rcvdByte)
{
//    if(!TestStyle) return;

    int length = 0;

    length = rcvdByte[1];

//    rcvdByte = rcvdByte.left(rcvdByte.length() -1);

    qDebug()<<"widgetBasewave, result:"<<rcvdByte.toHex();

    if((unsigned char)rcvdByte[0] == 0xff)
    {
        widgetInfo->hide();
//        QMessageBox::warning(this,tr("错误"),QString("无法链接到硬件："+rcvdByte.mid(1)),QMessageBox::tr("确定"));

        labInfo->setText("错误,无法链接到硬件!");
        widgetInfo->show();

        QTimer::singleShot(2000, this, SLOT(widgetInfoHide()));

//        TestStyle = false;

        sendBuf.clear();

        return;
    }

    if(length == 1)
    {
        if((unsigned char)rcvdByte[2] == 0x00)
        {
            //emit cmdSend();
            //qDebug()<<"pagehumiture"<<sendBuf.toHex();
        }
        else if((unsigned char)rcvdByte[2] == 0x01)
        {
            if(TestStyle)
            {
                labInfo->setText("基波输出设置成功！");
            }
            else if(!TestStyle)
            {
                labInfo->setText("已降源！");
            }
            widgetInfo->show();

            sendTcpBuf.append("1");
            sendTcpBuf.append("\r\n");

            widgetInfo->show();

            QTimer::singleShot(3000, this, SLOT(widgetInfoHide()));

            sendBuf.clear();

            //            isInTesting = false;

            //emit TcpSend();
        }
        sendTcpBuf.clear();
    }
    rcvdByte.clear();
}

QString widgetBasewave::change_data(int style, QString data)
{
    int i=0,j=0;
    QString data_changed = "";
    QString reData = "";
    for(i = 0;i < data.length();i++)
    {
        if(data[i] != '.')
        {
            data_changed.append(data[i]);
        }
    }

    if(style == 0)
    {
        if(data_changed.length() < 6)
        {
            j = data_changed.length();
            j = 6 - j;
            for(i=0;i<j;i++)
            {
                reData.append("0");
            }
            if(i == j)
            {
                reData.append(data_changed);
                return reData;
            }
        }
    }
    else if(style == 1)
    {
        if(data_changed.length() < 5)
        {
            j = data_changed.length();
            j = 5 - j;
            for(i=0;i<j;i++)
            {
                reData.append("0");
            }
            if(i == j)
            {
                reData.append(data_changed);
                return reData;
            }
        }
    }
    return 0;
}

void widgetBasewave::dataclear()
{
    labInfo->clear();
}

void widgetBasewave::DatatoHex(int Data,int style)
{
    QByteArray reData;
    unsigned char temp;
    if(style == 1)
    {
        temp = Data >> 16 & 0xFF;
        reData.append(temp);

        temp = Data >> 8 & 0xFF;
        reData.append(temp);

        temp = Data & 0xFF;
        reData.append(temp);
    }
    else if(style == 2)
    {

        temp = Data >> 8 & 0xFF;
        reData.append(temp);

        temp = Data & 0xFF;
        reData.append(temp);
    }

    sendBuf.append(reData);
}

void widgetBasewave::sendStopData()
{
    TestStyle = false;

    char zero = 0x00;

    sendBuf.clear();
    labInfo->setText("正在降源,请稍候...");
    widgetInfo->show();

    QTimer::singleShot(1000,widgetInfo,SLOT(hide()));

    sendBuf.append(0xAA);
    sendBuf.append(0x1B);
    for (int i = 0; i < 27; i++)
    {
        sendBuf.append(zero);
    }

    qDebug() << sendBuf.toHex() << sendBuf.length();
    //emit cmdSend();
}

void widgetBasewave::addOrder100(QLineEdit *line)
{
    int value = line->text().toDouble() * 100;
    sendBuf.append((value >> 8) & 0xff);
    sendBuf.append(value & 0xff);
}

void widgetBasewave::addOrder577(QLineEdit *line)
{
    int value = line->text().toDouble() * 100 / 1.7320508;
    sendBuf.append((value >> 8) & 0xff);
    sendBuf.append(value & 0xff);
}

void widgetBasewave::addOrder100_change(double value)
{
    int temp = (int)(value * 100);

    sendBuf.append((temp >> 8) & 0xff);
    sendBuf.append(temp & 0xff);
}

void widgetBasewave::startTest()
{
    TestStyle = true;

    sendBuf.clear();
    labInfo->setText("正在升源,请稍候...");
    widgetInfo->show();
    QTimer::singleShot(1000,widgetInfo,SLOT(hide()));

    double IAvalue = lineIAn->text().toDouble() * cboCurrentIA->currentText().toDouble();
    double IBvalue = lineIBn->text().toDouble() * cboCurrentIB->currentText().toDouble();
    double ICvalue = lineICn->text().toDouble() * cboCurrentIC->currentText().toDouble();

//---------------打系数----------------------
//    if (IAvalue > 120) CIA = 1;
//    else if (IAvalue > 12) CIA = 0.2;
//    else if (IAvalue > 6) CIA = 0.02;
//    else CIA = 0.01;

//    if (IBvalue > 120) CIB = 1;
//    else if (IBvalue > 12) CIB = 0.2;
//    else if (IBvalue > 6) CIB = 0.02;
//    else CIB = 0.01;

//    if (ICvalue > 120) CIC = 1;
//    else if (ICvalue > 12) CIC = 0.2;
//    else if (ICvalue > 6) CIC = 0.02;
//    else CIC = 0.01;

//    qDebug() << CIA << CIB << CIC;
//----------------------------------------

    sendBuf.append(0xAA);
    sendBuf.append(0x1B);

    if(cboVoltageUA->currentIndex() == 0)
    {
        addOrder100(lineUAn);
    }
    else
    {
        addOrder577(lineUAn);
    }

    if(cboVoltageUB->currentIndex() == 0)
    {
        addOrder100(lineUBn);
    }
    else
    {
        addOrder577(lineUBn);
    }

    if(cboVoltageUC->currentIndex() == 0)
    {
        addOrder100(lineUCn);
    }
    else
    {
        addOrder577(lineUCn);
    }


    addOrder100_change(IAvalue);
    addOrder100_change(IBvalue);
    addOrder100_change(ICvalue);

    addOrder100(LineUA);
    addOrder100(LineUB);
    addOrder100(LineUC);
    addOrder100(LineIA);
    addOrder100(LineIB);
    addOrder100(LineIC);
    sendBuf.append(0x01);
    addOrder100(Frequency);

    qDebug() << sendBuf.toHex() << sendBuf.length();
    //emit cmdSend();
}

void widgetBasewave::showTcpData(QString style, QString Netstr)
{
//    qDebug()<<"pagehumiture showTcpData(QString style, QString Netstr)"<<style<<Netstr;
//    QString m = "";
//    QStringList strlist;

//    strlist = Netstr.split('$');
//    qDebug()<<"pagehumiture showTcpData strlist.count()"<<strlist.count();


//    for(int i=0;i<strlist.count();i++)
//    {
//       m.append(strlist.at(i));
//       qDebug()<<"value of m"<<m;
//       if(i == 0)
//       {
//           if(style == "UA") cboVoltageUA->setCurrentIndex(m.toInt());
//           else if(style == "UB") cboVoltageUB->setCurrentIndex(m.toInt());
//           else if(style == "UC") cboVoltageUC->setCurrentIndex(m.toInt());
////           else if(style == "IA") cboCurrentIA->setText(m);
////           else if(style == "IB") cboCurrentIB->setText(m);
////           else if(style == "IC") cboCurrentIC->setText(m);
//       }
//       if(i == 1)
//       {
//           if(style == "UA") lineUAn->setText(QString::number(m.toFloat(),'f',0));
//           else if(style == "UB") lineUBn->setText(QString::number(m.toFloat(),'f',0));
//           else if(style == "UC") lineUCn->setText(QString::number(m.toFloat(),'f',0));
//           else if(style == "IA") lineIAn->setText(QString::number(m.toFloat(),'f',0));
//           else if(style == "IB") lineIBn->setText(QString::number(m.toFloat(),'f',0));
//           else if(style == "IC") lineICn->setText(QString::number(m.toFloat(),'f',0));
//       }
//       if(i == 2)
//       {
//           if(style == "UA") LineUA->setText(m);
//           else if(style == "UB") LineUB->setText(m);
//           else if(style == "UC") LineUC->setText(m);
//           else if(style == "IA") LineIA->setText(m);
//           else if(style == "IB") LineIB->setText(m);
//           else if(style == "IC") LineIC->setText(m);
//       }
//       m.clear();
//    }
}

void widgetBasewave::resolveTCPData(QString Netstr)
{
//    QString style = "";

//    QString m = "";
//    QStringList strlist;

//    lineActive = NULL;

//    Netstr = Netstr.simplified();
//    qDebug()<<"pagehumiture resolveTCPData Netstr:"<<Netstr;

//    if(Netstr.left(4) == "work" || Netstr.left(4) == "stop")
//    {
//        if(Netstr.left(4) == "work")
//        {
//            Netstr = Netstr.mid(4);
//            qDebug()<<"pagehumiture Netstr after mid(4)"<<Netstr;
//            strlist = Netstr.split('#');
//            qDebug()<<strlist.count();
//            for(int i=0;i<strlist.count();i++)
//            {
//                m.append(strlist.at(i));
//                qDebug()<<"value of m"<<m;
//                if(i < 6)
//                {
//                    if(i == 0) style = "UA";
//                    else if(i == 1) style = "UB";
//                    else if(i == 2) style = "UC";
//                    else if(i == 3) style = "IA";
//                    else if(i == 4) style = "IB";
//                    else if(i == 5) style = "IC";
//                    showTcpData(style,m);
//                }
//                else if(i == 6)
//                {
//                    Frequency->setText(m);
//                }
//                else if(i == 7)
//                {
//                    boxTest->setCurrentIndex(m.toInt());
//                }
//                else if(i == 8)
//                {
//                    boxVoltage->setCurrentIndex(m.toInt());
//                }
//                else if(i == 9)
//                {
//                    boxCurrent->setCurrentIndex(m.toInt());
//                }
//                else if(i == 10)
//                {
//                    boxPhase->setCurrentIndex(m.toInt());
//                }
//                m.clear();
//            }
//            startTest();
//        }
//        else if(Netstr.left(4) == "stop") sendStopData();
//    }
//    else if(Netstr.left(4) == "wave")
//    {
//        style.clear();
//        Netstr = Netstr.mid(4);
//        style = Netstr.left(2);
//        Netstr = Netstr.mid(2);
//        if(style == "UA")
//        {
//            if(Netstr.left(1) == "0") labPhaseAU->setChecked(false);
//            else if(Netstr.left(1) == "1") labPhaseAU->setChecked(true);
//        }
//        else if(style == "UB")
//        {
//            if(Netstr.left(1) == "0") labPhaseBU->setChecked(false);
//            else if(Netstr.left(1) == "1") labPhaseBU->setChecked(true);
//        }
//        else if(style == "UC")
//        {
//            if(Netstr.left(1) == "0") labPhaseCU->setChecked(false);
//            else if(Netstr.left(1) == "1") labPhaseCU->setChecked(true);
//        }
//        else if(style == "IA")
//        {
//            if(Netstr.left(1) == "0") labPhaseAI->setChecked(false);
//            else if(Netstr.left(1) == "1") labPhaseAI->setChecked(true);
//        }
//        else if(style == "IB")
//        {
//            if(Netstr.left(1) == "0") labPhaseBI->setChecked(false);
//            else if(Netstr.left(1) == "1") labPhaseBI->setChecked(true);
//        }
//        else if(style == "IC")
//        {
//            if(Netstr.left(1) == "0") labPhaseCI->setChecked(false);
//            else if(Netstr.left(1) == "1") labPhaseCI->setChecked(true);
//        }
//    }
//    Netstr = Netstr.mid(Netstr.length());

//    sendTcpBuf.append("1");
//    sendTcpBuf.append("\r\n");
    //emit TcpSend();
}

void widgetBasewave::UQuickSet(QString str)
{
    lineUAn->setText(str);
    lineUBn->setText(str);
    lineUCn->setText(str);

}

void widgetBasewave::IQuickSet(QString str)
{
    lineIAn->setText(str);
    lineIBn->setText(str);
    lineICn->setText(str);
}

void widgetBasewave::PQuickSet(QString str)
{
    float temp = str.toFloat();

    float tema = LineUA->text().toFloat() - temp;
    float temb = LineUB->text().toFloat() - temp;
    float temc = LineUC->text().toFloat() - temp;

    if(tema < 0) tema += 360;
    if(temb < 0) temb += 360;
    if(temc < 0) temc += 360;

    LineIA->setText(QString::number(tema,'f',2));
    LineIB->setText(QString::number(temb,'f',2));
    LineIC->setText(QString::number(temc,'f',2));
}
